<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>开标</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" id="name" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">开标结果</label>
                        <div class="layui-input-block">
                            <select id="state" name="state" lay-filter="state"></select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item state_win layui-hide">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">中标日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="kai_windt" id="kai_windt" placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">中标价(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="kai_fee" name="kai_fee" placeholder="请输入" clearable autocomplete="off" class="layui-input moneyinput">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">开标结果说明</label>
                        <div class="layui-input-block">
                            <textarea id="kai_explain" name="kai_explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>投标单位明细</span></div>
                        <div>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>投标单位</th>
                            <th>性质</th>
                            <th>开标结果</th>
                            <th>开标说明</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_bid_kai,data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;

                $('#name').val(data.name);
                $('#kai_windt').val(data.kai_windt);
                $('#kai_fee').val(data.kai_fee);
                $('#kai_explain').val(data.kai_explain);


                var kai_guo_str = '<option value="">请选择</option>';
                var kai_guo = res.data.kai_guo;
                for (var key in kai_guo){
                    if(data.state == key){
                        kai_guo_str += '<option value="'+key+'" selected>'+kai_guo[key]+'</option>';
                    }else{
                        kai_guo_str += '<option value="'+key+'">'+kai_guo[key]+'</option>';
                    }
                }
                $("#state").html(kai_guo_str);

                if(data.state == 3){
                    $(".state_win").removeClass('layui-hide');
                }else{
                    $(".state_win").addClass('layui-hide');
                }


                var list = res.data.list;
                if(list.length > 0){
                    for (var key in list){
                        var str = '';
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" class="layui-input touunit disabled" id="'+list[key].id+'" disabled  value="'+list[key].touunit+'"></td>';
                        str += '<td><input type="text" class="layui-input touxing_name disabled" disabled value="'+list[key].touxing_name+'"></td>';

                        str += '<td><select class="layui-select kaiguo">';
                        str += '<option value="">请选择</option>';
                        if(list[key].kaiguo == '中标'){
                            str += '<option value="中标" selected>中标</option>';
                            str += '<option value="未中标">未中标</option>';
                        }else if(list[key].kaiguo == '未中标'){
                            str += '<option value="中标">中标</option>';
                            str += '<option value="未中标" selected>未中标</option>';
                        }else{
                            str += '<option value="中标">中标</option>';
                            str += '<option value="未中标">未中标</option>';
                        }
                        str += '</select></td>';

                        str += '<td><input type="text" class="layui-input kaishuo" value="'+list[key].kaishuo+'"></td>';
                        str += '</tr>';
                        $("#list_list").append(str);
                    }
                }


                public_upload_file_load('upload_files', res.data.files);
                render_date();
                render_from();
                initParseMoney();
            }
        });

        form.on('select(state)', function(data){
            if(data.value == 3){
                $(".state_win").removeClass('layui-hide');
            }else{
                $(".state_win").addClass('layui-hide');
            }
        });


        form.on('submit(add)', function(data){
            var validRes = $("#commentForm").valid();
            if(!validRes){return false;}

            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var id = $(this).find('.touunit').attr('id') || 0;
                    var kaiguo = $(this).find('.kaiguo').val();
                    var kaishuo = $(this).find('.kaishuo').val();

                    var index = i + 1;

                    list.push({
                        id:id,
                        kaiguo:kaiguo,
                        kaishuo:kaishuo,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data = parseMoney(data);
            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            facade.ajax({url: ask_bid_kai, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    facade.success("提交成功!");
                    if(res.data.enginid > 0){
                        facade.success("项目信息已生成，请至项目管理查看!");
                    }
                    setTimeout(() => {
                        try {
                            parent.layui.table.reload('datalist');
                        }catch (e) {

                        }
                        layer_close_opt();
                    }, 3000);
                }
            });

            return false;
        });

    });

</script>
</body>
</html>
